<?php

namespace app\api\model;

use app\service\im\Group as ServiceGroup;
use app\service\im\Group;
use think\Model;

class ImGroup extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:群列表
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function GetGroupList($params)
    {
        $where = [
            'g.uid' => $params['uid'],
            'g.group_category_id' => $params['group_category_id'],
            'g.status' => 1
        ];
        $list = ImGroup::alias('g')
            ->join('user u', 'u.id=g.uid', 'LEFT')
            ->join('im_game_category gc', 'gc.id=g.group_category_id', 'LEFT')
            ->where($where)
            ->field('u.id as uid ,u.username, g.*,gc.category_name')
            ->order('g.id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return $list;
    }

    /**
     * Notes:创建群插入群用户
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function InsertCreateGroup($params)
    {
        //不存在群时添加记录
        $this->data([
            'uid' => $params['uid'],
            'GroupId' => $params['group_id'],
            'url'=>$params['url'],
            'group_name' => $params['group_name'],
        ]);
        $result = $this->save();
        if ($result) {
            return $this->id;
        } else {
            return false;
        }
    }


    /**
     * Notes:插入群用户
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function InsertGroup($uid, $group_id, $group_name, $channel_id, $wheat_duration, $num, $game_category_id, $channel_announcement)
    {
        //不存在群时添加记录
        $this->data([
            'uid' => $uid,
            'GroupId' => $group_id,
//            'name'=>$group_name,
            'group_name' => $group_name,
//            'channel_id'=>$channel_id,
//            'wheat_duration'=>$wheat_duration,
//            'num'=>$num,
//            'game_category_id'=>$game_category_id,
//            'channel_announcement'=>$channel_announcement,
        ]);
        $result = $this->save();
        if ($result) {
            return $this->id;
        } else {
            return false;
        }
    }

    /**
     * Notes:编辑群数据
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function UpdateGroup($params)
    {
        $this->where(['uid' => $params['uid'], 'GroupId' => $params['group_id']])->update(['url' => $params['url'], 'group_name' => $params['group_name'], 'status' => 1, 'update_time' => time()]);// 1是, 2否
        return true;
    }

    /**
     * Notes:删除群数据
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function DelGroup($params)
    {
        $this->where(['GroupId' => $params['group_id']])->update(['status' => 2, 'update_time' => time()]);// status 1正常，2解散
        return true;
    }

    /**
     * Notes:创建房间频道im群(群主创建房间)
     * User: tangfen
     * Date: 2023/12/27
     * Time: 15:49
     * @param $params
     */
    public function addRtcGroup($uid, $group_name, $channel_id, $wheat_duration, $num, $game_category_id, $channel_announcement)
    {
        $uidArray = explode(',', $uid);
        $member = [];
        //初始群成员列表，最多100个
        if (!empty($uidArray)) {
            foreach ($uidArray as $k => $v) {
                $member['Member_Account'] = $v;
                $member['Role'] = 'Admin';
                $uidArray[$k] = $member;
            }
        }
        //创建腾讯im群 （自己创建的不能自己加入，默认自己在群里面了）
        $res = new ServiceGroup();
        $data = $res->createGroup($uid, 1, $group_name, $uidArray, $channel_announcement, '', $Notification = '');

        if ($data['ActionStatus'] == 'OK') {

            $result = (new ImGroup)->InsertGroup($uid, $data['GroupId'], '频道im群' . $group_name, $channel_id, $wheat_duration, $num, $game_category_id, $channel_announcement);//频道加im群表入库

            if ($result) {
                $GroupId['group_id'] = $data['GroupId'];
                $groupUser = (new ImGroupUser)->AddGroupUser($GroupId, $uidArray);//加群成员数据
                if ($groupUser) {
                    return $result;
                } else {
                    return_ajax(0, '频道加入im群成员入库失败');
                }
            }
            return_ajax(0, '频道加im群入库失败');
        } else {
            return_ajax(0, '创建第三方腾讯im群失败', $data);
        }
    }


    /**
     * Notes:加入房间频道（用户加入房间）
     * User: tangfen
     * Date: 2023/12/28
     * Time: 13:18
     */
    public function addGroupMember($params)
    {
        $uidArray = explode(',', $params['uid']);
        $member = [];
        //初始群成员列表，最多100个
        if (!empty($uidArray)) {
            foreach ($uidArray as $k => $v) {
                $member['Member_Account'] = $v;
//                $member['Role'] = 'Admin';
                $uidArray[$k] = $member;
            }
        }
        //加入腾讯im群
        $res = new Group();
        $data = $res->addGroupMember($params['group_id'], $uidArray);
        if ($data['ActionStatus'] == 'OK') {
            //加im群成员数据
            $groupUser = (new ImGroupUser)->AddGroupUser($params, $uidArray);

            //加入房间成员数据
            $rtcPlay = new RtcPlay();
            $rtcPlay->insertRtcPlay($params, 2);

            if ($groupUser) {
                return 'true';
            } else {
                return_ajax(0, '房间加入im群成员入库失败');
            }
            return_ajax(0, '房间加im群入库失败');
        } else {
            return_ajax(0, '加入腾讯im群失败');
        }

    }


}